<?php
/**
 * Created by PhpStorm.
 * User: woddp
 * Date: 2017/2/12
 * Time: 12:15
 */

namespace Admin\Controller;

class UploadController extends CommonController {

    public function imgUpload() {
        $type=I('get.type');//上传的图片类型
        $upload           = new Upload();// 实例化上传类
        $upload->maxSize  = 3145728;// 设置附件上传大小
        $upload->exts     = ['jpg', 'gif', 'png', 'jpeg'];// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        $imgUrl = getRootUrl() . '/Public/Uploads/' . $info['file']['savepath'] . $info['file']['savename'];
        if(!$info) {// 上传错误提示错误信息
            $this->ajaxReturn(['status' => '0', 'msg' => $upload->getError()]);
        } else {// 上传成功
           $id= $this->saveImg($imgUrl,$type);
            $this->ajaxReturn(['status' => '1', 'msg' => ['id'=>$id,'type'=>$type,'img'=>$imgUrl]]);
        }
    }

    public function saveImg($imgUrl,$type) {
        $imagesModel = M('images');
        $type=empty($type)?2:$type; //图片类型，1 logo 2.图集
        $data        = [
            'type' =>$type,
            'path' => $imgUrl,
        ];
        
        return  $imagesModel->data($data)->add();
    }

    /**
     * 编辑器上传
     */
    public function editUpload() {
        $upload           = new Upload();// 实例化上传类
        $upload->maxSize  = 3145728;// 设置附件上传大小
        $upload->exts     = ['jpg', 'gif', 'png', 'jpeg'];// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();

        if(!$info) {// 上传错误提示错误信息
            $this->ajaxReturn("error|" . $upload->getError());
        } else {// 上传成功
            echo getRootUrl() . '/Public/Uploads/' . $info['wangEditorH5File']['savepath'] . $info['wangEditorH5File']['savename'];
            return;
        }
    }

}